import { Injectable, OnModuleInit } from '@nestjs/common'
import { ProjectCompanyRepo, ProjectRepo } from '@/entities'
import { timestampFormat } from '@/util'

const defaultProject = [
    { projectid: 'edp', projectName: '企业数字化平台', projectType: 1 },
    { projectid: 'gis', projectName: '智能商贸物流平台', projectType: 1 },
]

@Injectable()
export class ProjectService implements OnModuleInit {
    constructor(private readonly projectRepo: ProjectRepo, private readonly projectCompanyRepo: ProjectCompanyRepo) {}
    public async onModuleInit() {
        for await (const dp of defaultProject) {
            const ext = await this.projectRepo.findOne({ projectid: dp.projectid })
            const adminExt = await this.projectCompanyRepo.findOne({ projectid: dp.projectid, companyid: '0000' })
            if (!ext) {
                await this.projectRepo.save(this.projectRepo.create({ ...dp, au: 'system', ct: timestampFormat() }))
            }
            if (!adminExt) {
                await this.projectCompanyRepo.save(
                    this.projectCompanyRepo.create({
                        projectid: dp.projectid,
                        companyid: '0000',
                        type: 0,
                    }),
                )
                await this.projectCompanyRepo.save(
                    this.projectCompanyRepo.create({
                        projectid: dp.projectid,
                        companyid: '0000',
                        type: 1,
                    }),
                )
            }
        }
    }
}
